<?php
namespace app\bidco\controller;


class Aucting extends \Common {

    private $spec = ["size" => "规格"];

    # 登录重写
    public function Login(){}
    # 获取信息重写
    public function Info(){}
    # 注销重写
    public function Logout(){}

    # 获取价格进度
    public function GetBidcoCurr(){
        if(isset($_GET["id"])){
            $lid = $_GET["id"];

            $lot= db("lot")
                ->where("is_over", 0)
                ->where("is_del", 0)
                ->where("lid", $lid)
                ->find();

            if(!empty($lot)){
                # 获取正在拍卖的商品
                $goods = db("goods")
                    ->where("lid", $lid)
                    ->where("state", 1)
                    ->find();

                # 近期三条商品
                $RecentLimit = db("goods")
                    ->where("lid", $_GET["id"])
                    ->where(function ($query){
                        $query->where("state", 3)->whereOr("state", 4)->whereOr("state", 5)->whereOr("state", 6);
                    })
                    ->order("clinch_time desc")
                    ->limit(3)
                    ->select();

                $image = $this->SearchImg($lot["lid"], $goods["number"]);

                $num = $this->GetPeople($goods);
                $bids = [];
                if(!empty($goods)){
                    if($goods["final_price"] != 0){
                        $bids[] = [
                            "num" => (is_bool($num) && !$num) ? 0 : $num,
                            "bid" => $goods["final_price"]
                        ];
                    }
                    if($goods["revoke_bid"] != ""){
                        $revoke = json_decode($goods["revoke_bid"], true);
                        $revoke_num = $this->GetPeople($revoke);
                        $bids[] = [
                            "num" => (is_bool($revoke_num) && !$revoke_num) ? 0 : $revoke_num,
                            "bid" => $revoke["final_price"]
                        ];
                    }
                }

                $recent = [];
                if(!empty($RecentLimit)){
                    foreach ($RecentLimit as $value){
                        $number = $this->GetPeople($value);

                        $recent[] = [
                            "buyer_num" => $value["final_price"] == 0 ? "" : (is_bool($number) && !$number) ? $value["peo_num"] : $number,
                            "buyer_way" => $value["final_price"] == 0 ? "" : (is_bool($number) && !$number) == 0 ? "现场" : "网络",
                            "final" => $this->GetFinal($value),
                            "lot" => $value["number"],
                        ];
                    }
                }

                $array = [
                    "bids" => $bids,
                    "goods" => empty($goods) ? false : [
                        "id" => $goods["gid"],
                        "lot" => $goods["number"],
                        "cover" => $image[0],
                        "curr" => $goods["final_price"] == 0 ? $goods["price"] : $goods["final_price"],
                        "curr_buyer" => $goods["final_price"] == 0 ? "" : (is_bool($num) && !$num) ? $goods["peo_num"] : $num,
                        "final" => $this->GetFinal($goods, false),
                        "name" => $goods["name"],
                        "start" => (float)$goods["price"],
                        "state" => ($goods["state"] == 0 || $goods["state"] == 1) ? $goods["state"] : 2,
                        "spec" => $this->Spec($goods, $this->spec),
                    ],
                    "recent" => $recent,
                    "name" => $lot["name"]
                ];
                Code(200, false, $array);
            }else{
                Code(403, "专场不存在");
            }
        }else{
            Code(403, "服务器出错");
        }
    }

    # 代拍公司出价
    public function PostCoBid(){
        $aid = $this->token["id"];
        $admin = db("admin")->where("aid", $aid)->find();
        if(isset($_POST["id"]) && isset($_POST["bid"])){
            $gid = $_POST["id"];
            $bid = $_POST["bid"];

            $good = db("goods")->where("gid", $gid)->find();
            if(!empty($good)){
                if(($good["final_price"] == 0 && ($good["price"] <= $_POST["bid"])) || ($good["final_price"] != 0 && ($good["final_price"] < $_POST["bid"]))){
                    $send = [
                        "type" => "bidnet",
                        "lot_id" => $good["lid"],
                        "num" => $admin["name"],
                        "bid" => $bid,
                    ];
                    $this->sendTogrounp($send, $good["lid"]);
                    Code(20, "已提交，等待出价审核", (Object)[]);
                }else{
                    Code(403, "出价不能低于当前价格");
                }
            }else{
                Code(403, "服务器出错");
            }
        }else{
            Code(403, "服务器出错");
        }
    }

}